package com.yuhang.learnandroid.chaper05.quiz

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Intent
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewAnimationUtils
import com.yuhang.learnandroid.R
import kotlinx.android.synthetic.main.activity_cheat.*

class CheatActivity : AppCompatActivity() {
    private val EXTRA_ANSWER_IS_TRUE = "com.answer_is_true"
    private val EXTRA_ANSWER_SHOWN = "com.answer_shown"

    var answerIsTrue: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cheat)

        answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false)
        var idx = intent.getIntExtra("idx", 0)
        show_answer_button.setOnClickListener{
            answer_text_view.setText(if(answerIsTrue) R.string.true_button else R.string.false_button)
            setAnswerShownResult(idx, true)

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                val cx = show_answer_button.width / 2
                val cy = show_answer_button.height / 2
                var radius = show_answer_button.width.toFloat()
                var anim = ViewAnimationUtils.createCircularReveal(show_answer_button, cx, cy, radius, 0.0f)
                anim.addListener(object: AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator?) {
                        super.onAnimationEnd(animation)
                        show_answer_button.visibility = View.INVISIBLE
                    }
                })
                anim.start()
            }else{
                show_answer_button.visibility = View.INVISIBLE
            }
        }


    }

    private fun setAnswerShownResult(idx: Int, isAnswerShown: Boolean){
        var data = Intent()
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown)
        data.putExtra("idx", idx)
        setResult(RESULT_OK, data)
    }
}
